package com.longcai.cm.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.longcai.cm.base.bean.Pageable;
import com.longcai.cm.exception.SysRuntimeException;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Origin
 * modify By LBX
 */
public class RequestUtil {


    @SuppressWarnings({"unchecked"})
    public static Pageable getPageable(HttpServletRequest request) {

        String searchString = request.getParameter("searchParam");
        Map<String, Object> resultMap = JSON.parseObject(searchString, HashMap.class);

        String orderString = request.getParameter("orderParam");
        Map<String, Boolean> orderMap = JSON.parseObject(orderString, LinkedHashMap.class);

        int offset = 0;
        int pageSize = 10;

        if (request.getParameter("pageSize") != null && request.getParameter("pageSize") != "") {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        } else if (request.getParameter("limit") != null && request.getParameter("limit") != "") {
            pageSize = Integer.parseInt(request.getParameter("limit"));
        }

        if (request.getParameter("offset") != null && request.getParameter("offset") != "") {
            offset = Integer.parseInt(request.getParameter("offset"));
            return new Pageable(offset / pageSize + 1, pageSize, resultMap, orderMap);
        } else if (request.getParameter("pageNum") != null && request.getParameter("pageNum") != "") {
            return new Pageable(Integer.parseInt(request.getParameter("pageNum")), pageSize, resultMap, orderMap);
        } else if (request.getParameter("page") != null && request.getParameter("page") != "") {
            return new Pageable(Integer.parseInt(request.getParameter("page")), pageSize, resultMap, orderMap);
        } else {
            return new Pageable(offset + 1, pageSize, resultMap, orderMap);
        }

    }

    /**
     * 根据 httpServletRequest 获取请求的ip地址信息
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            final boolean ipNotNull = ipAddress == null || ipAddress.length() == 0;
            if (ipNotNull || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipNotNull || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipNotNull || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if ("127.0.0.1".equals(ipAddress)) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                        throw new SysRuntimeException("来源地未知");
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            // "***.***.***.***".length()
            if (ipAddress != null && ipAddress.length() > 15) {
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }

        return ipAddress;
    }

}
